package com.ruoyi.web.controller.map;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.map.demo.util.HttpUtils;
import com.ruoyi.map.domain.vo.VillageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.map.domain.VillageInfor;
import com.ruoyi.map.service.IVillageInforService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保障房建筑信息数据Controller
 *
 * @author dihang
 * @date 2023-07-24
 */
@RestController
@Api(value = "保障房地图小区数据信息",tags = {"保障房地图小区数据信息"})
@RequestMapping("/map/infor")
public class VillageInforController extends BaseController
{
    @Autowired
    private IVillageInforService villageInforService;



    /**
     * 查询保障房建筑信息数据列表
     */
    @ApiOperation(value = "根据小区名称搜索标点")
    @GetMapping("/biaodian")
    public AjaxResult search(String name){
        VillageVo villageVo = villageInforService.selectName(name);
        return AjaxResult.success(villageVo);
    }

//    @ApiOperation(value = "身份验证")
//    @PostMapping("/test/{name}/{idNo}")
//    public AjaxResult ceshi(@PathVariable String name,@PathVariable String idNo) {
//        String host = "https://idenauthen.market.alicloudapi.com";
//        String path = "/idenAuthentication";
//        String method = "POST";
//        String appcode = "f13835a209b24e74a243105436ab72e5";
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + appcode);
//        //根据API的要求，定义相对应的Content-Type
//        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        Map<String, String> querys = new HashMap<String, String>();
//        Map<String, String> bodys = new HashMap<String, String>();
//        bodys.put("idNo", idNo);
//        bodys.put("name", name);
//        Object result = new Object();
//
//
//        try {
//            /**
//             * 重要提示如下:
//             * HttpUtils请从
//             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
//             * 下载
//             *
//             * 相应的依赖请参照
//             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
//             */
//            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
//            //获取response的body
//            result = EntityUtils.toString(response.getEntity());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    return AjaxResult.success(result);
//    }

    public static void main(String[] args) {
        String host = "https://idenauthen.market.alicloudapi.com";
        String path = "/idenAuthentication";
        String method = "POST";
        String appcode = "f13835a209b24e74a243105436ab72e5";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("idNo", "130627200007183216");
        bodys.put("name", "邸航");
        Object result = new Object();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
//            System.out.println(EntityUtils.toString(response.getEntity()));
            result = EntityUtils.toString(response.getEntity());
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 分页条件查询保障房建筑信息数据列表
     */
    @ApiOperation(value = "分页条件查询保障房建筑信息数据列表")
//    @PreAuthorize("@ss.hasPermi('map:infor:list')")
    @GetMapping("/list/page")
    public IPage<VillageInfor> listPages(VillageInfor villageInfor)
    {
        IPage<VillageInfor> page = villageInforService.villageSearch(villageInfor);
        return page;
    }

    /**
     * 查询保障房建筑信息数据列表
     */
    @ApiOperation(value = "查询保障房建筑信息数据列表")
//    @PreAuthorize("@ss.hasPermi('map:infor:list')")
    @GetMapping("/list")
    public AjaxResult list()
    {
        List<VillageInfor> list = villageInforService.selectVillageInforList();
        return AjaxResult.success(list);
    }

    /**
     * 导出保障房建筑信息数据列表
     */
//    @ApiOperation(value = "导出保障房建筑信息数据列表")
    @PreAuthorize("@ss.hasPermi('map:infor:export')")
    @Log(title = "保障房建筑信息数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<VillageInfor> list = villageInforService.list(null);
        if (list.isEmpty()){
            throw new BaseException("保障房建筑小区数据为空！");
        }
        ExcelUtil<VillageInfor> util = new ExcelUtil<VillageInfor>(VillageInfor.class);
        util.exportExcel(response, list, "保障房建筑信息数据数据");
    }

    /**
     * 获取保障房建筑信息数据详细信息
     */
    @ApiOperation(value = "获取保障房建筑信息数据详细信息")
    @PreAuthorize("@ss.hasPermi('map:infor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        VillageInfor villageInfor = villageInforService.selectById(id);
        return AjaxResult.success(villageInfor);
    }

    /**
     * 新增保障房建筑信息数据
     */
    @ApiOperation(value = "新增保障房建筑信息数据")
    @PreAuthorize("@ss.hasPermi('map:infor:add')")
    @Log(title = "保障房建筑信息数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VillageInfor villageInfor) {
        villageInforService.addNew(villageInfor);
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改保障房建筑信息数据
     */
    @ApiOperation(value = "修改保障房建筑信息数据")
    @PreAuthorize("@ss.hasPermi('map:infor:edit')")
    @Log(title = "保障房建筑信息数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VillageInfor villageInfor) {
        return toAjax(villageInforService.updateById(villageInfor));
    }

    /**
     * 删除保障房建筑信息数据
     */
    @ApiOperation(value = "删除保障房建筑信息数据")
    @PreAuthorize("@ss.hasPermi('map:infor:remove')")
    @Log(title = "保障房建筑信息数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids) {
        villageInforService.removeById(ids);
        return AjaxResult.success("删除成功！");
    }
}
